﻿@model CurrencySearchModel

@{
    //page title
    ViewBag.PageTitle = T("Admin.Configuration.Currencies").Text;
    //active menu item (system name)
    NopHtml.SetActiveMenuItemSystemName("Currencies");
}

<form asp-controller="Currency" asp-action="List" method="post">
    <div class="content-header clearfix">
        <h1 class="float-left">
            @T("Admin.Configuration.Currencies")
        </h1>
        <div class="float-right">
            <button type="submit" name="save" class="btn btn-primary">
                <i class="far fa-floppy-disk"></i>
                @T("Admin.Common.Save")
            </button>
            <a asp-controller="Currency" asp-action="List" asp-route-liveRates="true" class="btn btn-success">
                <i class="fas fa-list-check"></i>
                @T("Admin.Configuration.Currencies.GetLiveRates")
            </a>
            <a asp-action="Create" class="btn btn-primary">
                <i class="fas fa-square-plus"></i>
                @T("Admin.Common.AddNew")
            </a>
            @await Component.InvokeAsync(typeof(AdminWidgetViewComponent), new { widgetZone = AdminWidgetZones.CurrencyListButtons, additionalData = Model })
        </div>
    </div>

    <section class="content">
        <div class="container-fluid">
            <div class="form-horizontal">
                <div class="form-horizontal">
                    <div class="cards-group">
                        <div class="card card-default">
                            <div class="card-body">
                                <div class="form-group row">
                                    <div class="col-md-3">
                                        <nop-label asp-for="ExchangeRateProviderModel.ExchangeRateProvider" />
                                    </div>
                                    <div class="col-md-9">
                                        <nop-select asp-for-name="ExchangeRateProviderModel.ExchangeRateProvider" asp-items="Model.ExchangeRateProviderModel.ExchangeRateProviders" />
                                        <span asp-validation-for="ExchangeRateProviderModel.ExchangeRateProvider"></span>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-md-3">
                                        <nop-label asp-for="ExchangeRateProviderModel.AutoUpdateEnabled" />
                                    </div>
                                    <div class="col-md-9">
                                        <nop-editor asp-for="ExchangeRateProviderModel.AutoUpdateEnabled" />
                                        <span asp-validation-for="ExchangeRateProviderModel.AutoUpdateEnabled"></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="card card-default">
                            <div class="card-body">
                                <nop-doc-reference asp-string-resource="@T("Admin.Documentation.Reference.Currencies", Docs.Currencies + Utm.OnAdmin)" />

                                @await Html.PartialAsync("Table", new DataTablesModel
                            {
                                Name = "currencies-grid",
                                UrlRead = new DataUrl("ListGrid", "Currency", null),
                                Length = Model.PageSize,
                                LengthMenu = Model.AvailablePageSizes,
                                ColumnCollection = new List<ColumnProperty>
                                {
                                    new ColumnProperty(nameof(CurrencyModel.Name))
                                    {
                                        Title = T("Admin.Configuration.Currencies.Fields.Name").Text
                                    },
                                    new ColumnProperty(nameof(CurrencyModel.CurrencyCode))
                                    {
                                        Title = T("Admin.Configuration.Currencies.Fields.CurrencyCode").Text,
                                        Width = "100"
                                    },
                                    new ColumnProperty(nameof(CurrencyModel.Rate))
                                    {
                                        Title = T("Admin.Configuration.Currencies.Fields.Rate").Text,
                                        Width = "80"
                                    },
                                    new ColumnProperty(nameof(CurrencyModel.IsPrimaryExchangeRateCurrency))
                                    {
                                        Title = T("Admin.Configuration.Currencies.Fields.IsPrimaryExchangeRateCurrency").Text,
                                        Width = "100",
                                        ClassName =  NopColumnClassDefaults.CenterAll,
                                        Render = new RenderBoolean()
                                    },
                                    new ColumnProperty(nameof(CurrencyModel.Id))
                                    {
                                        Title = T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryExchangeRateCurrency").Text,
                                        ClassName =  NopColumnClassDefaults.CenterAll,
                                        Render = new RenderButtonCustom(NopButtonClassDefaults.Olive, T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryExchangeRateCurrency").Text)
                                        {
                                            OnClickFunctionName =  "markAsPrimaryExchangeRateCurrency"
                                        }
                                    },
                                    new ColumnProperty(nameof(CurrencyModel.IsPrimaryStoreCurrency))
                                    {
                                        Title = T("Admin.Configuration.Currencies.Fields.IsPrimaryStoreCurrency").Text,
                                        Width = "100",
                                        ClassName =  NopColumnClassDefaults.CenterAll,
                                        Render = new RenderBoolean()
                                    },
                                    new ColumnProperty(nameof(CurrencyModel.Id))
                                    {
                                        Title = T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryStoreCurrency").Text,                                        
                                        ClassName =  NopColumnClassDefaults.CenterAll,
                                        Render = new RenderButtonCustom(NopButtonClassDefaults.Olive, T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryStoreCurrency").Text)
                                        {
                                            OnClickFunctionName = "markAsPrimaryStoreCurrency"
                                        }
                                    },
                                    new ColumnProperty(nameof(CurrencyModel.DisplayOrder))
                                    {
                                        Title = T("Admin.Configuration.Currencies.Fields.DisplayOrder").Text,
                                        Width = "50",
                                        ClassName =  NopColumnClassDefaults.CenterAll
                                    },
                                    new ColumnProperty(nameof(CurrencyModel.Published))
                                    {
                                        Title = T("Admin.Configuration.Currencies.Fields.Published").Text,
                                        Width = "50",
                                        ClassName =  NopColumnClassDefaults.CenterAll,
                                        Render = new RenderBoolean()
                                    },
                                    new ColumnProperty(nameof(CurrencyModel.Id))
                                    {
                                        Title = T("Admin.Common.Edit").Text,
                                        Width = "80",
                                        ClassName =  NopColumnClassDefaults.Button,
                                        Render = new RenderButtonEdit(new DataUrl("~/Admin/Currency/Edit"))
                                    }
                                }
                            })


                                <button type="submit" class="btn btn-default" id="btnMarkPERC" style="display:none" />
                                <button type="submit" class="btn btn-default" id="btnMarkPSC" style="display:none" />

                                <script>
                        var selectedId;

                        $(function() {

                            $("#btnMarkPSC-action-confirmation-submit-button").on("click", function () {
                                var postData = {
                                    id: selectedId
                                };
                                addAntiForgeryToken(postData);
                                $.ajax({
                                    cache: false,
                                    type: "POST",
                                    url: "@(Url.Action("MarkAsPrimaryStoreCurrency", "Currency"))",
                                    data: postData,
                                    success: function (data, textStatus, jqXHR) {
                                        updateTable('#currencies-grid');
                                    },
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        $("#currencyAlert").trigger("click");
                                    }
                                });
                                $('#btnMarkPSC-action-confirmation').modal('toggle');
                            });

                            $("#btnMarkPERC-action-confirmation-submit-button").on("click", function () {
                                var postData = {
                                    id: selectedId
                                };
                                addAntiForgeryToken(postData);
                                $.ajax({
                                    cache: false,
                                    type: "POST",
                                    url: "@(Url.Action("MarkAsPrimaryExchangeRateCurrency", "Currency"))",
                                    data: postData,
                                    success: function (data, textStatus, jqXHR) {
                                        updateTable('#currencies-grid');
                                    },
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        $("#currencyAlert").trigger("click");
                                    }
                                });
                                $('#btnMarkPERC-action-confirmation').modal('toggle');
                            });

                        });

                        function markAsPrimaryExchangeRateCurrency(id) {
                            selectedId = id;
                            $("#btnMarkPERC").trigger("click");
                        };

                        function markAsPrimaryStoreCurrency(id) {
                            selectedId = id;
                            $("#btnMarkPSC").trigger("click");
                        };
                                </script>
                            </div>
                        </div>
                        @if (Model.ExchangeRateProviderModel.ExchangeRates?.Any() ?? false)
                        {
                            <div class="card card-default">
                                <div class="card-header">
                                    @T("Admin.Configuration.Currencies.LiveRates")
                                </div>
                                <div class="card-body">

                                    <a onclick="applyCurrencyRate()" class="btn bg-olive">@T("Admin.Configuration.Currencies.ApplyRate.All")</a>
                                    @await Html.PartialAsync("Table", new DataTablesModel
                                    {
                                        Name = "rate-grid",
                                        Data = "currencyrates_source",
                                        ServerSide = false,
                                        Paging = false,
                                        Length = Model.PageSize,
                                        LengthMenu = Model.AvailablePageSizes,
                                        ColumnCollection = new List<ColumnProperty>
                                        {
                                            new ColumnProperty(nameof(CurrencyModel.CurrencyCode))
                                            {
                                                Title = T("Admin.Configuration.Currencies.Fields.CurrencyCode").Text,
                                                Width = "300"
                                            },
                                            new ColumnProperty(nameof(CurrencyModel.Rate))
                                            {
                                                Title = T("Admin.Configuration.Currencies.Fields.Rate").Text,
                                                Width = "300"
                                            },
                                            new ColumnProperty(nameof(CurrencyModel.CurrencyCode))
                                            {
                                                Title = T("Admin.Configuration.Currencies.ApplyRate").Text,
                                                Width = "200",
                                                ClassName = NopColumnClassDefaults.Button,
                                                Render = new RenderCustom("renderColumnApplyRate")
                                            }
                                        }
                                    })
                                    <script>
                                function renderColumnApplyRate(data, type, row, meta) {
                                    return '<a onclick="applyCurrencyRate(\'' +
                                        row.Rate +
                                        '\',\'' +
                                        row.CurrencyCode +
                                        '\')" class="btn bg-olive">@T("Admin.Configuration.Currencies.ApplyRate")</a>';
                                }

                                function applyCurrencyRate(rate, currencyCode) {
                                    var postData = {
                                        rateModels: (rate && currencyCode) ? [{ CurrencyCode: currencyCode, Rate: rate }] : currencyrates_source
                                    };
                                    addAntiForgeryToken(postData);
                                    $.ajax({
                                        cache: false,
                                        type: "POST",
                                        url: "@(Url.Action("ApplyRates"))",
                                        data: postData,
                                        success: function (data, textStatus, jqXHR) {
                                            updateTable('#currencies-grid');
                                        },
                                        error: function (jqXHR, textStatus, errorThrown) {
                                            $("#currencyAlert").trigger("click");
                                        }
                                    });
                                }

                                //local datasource
                                var currencyrates_source = [
                                    @for (var i = 0; i < Model.ExchangeRateProviderModel.ExchangeRates.Count; i++)
                                    {
                                        var rate = Model.ExchangeRateProviderModel.ExchangeRates[i];
                                        <text>
                                            {
                                                CurrencyCode:
                                                    "@(Html.Raw(JavaScriptEncoder.Default.Encode(rate.CurrencyCode)))",
                                                Rate:
                                                    "@(Html.Raw(JavaScriptEncoder.Default.Encode(rate.Rate.ToString())))"
                                            }
                                        </text>
                                        if (i != Model.ExchangeRateProviderModel.ExchangeRates.Count - 1)
                                        {
                                            <text>,</text>
                                        }
                                    }
                                ];
                                    </script>
                                </div>
                            </div>
                        }
                    </div>
                </div>
            </div>
        </div>
    </section>
</form>
<nop-action-confirmation asp-button-id="btnMarkPERC" asp-additional-confirm="Admin.Configuration.Currencies.AdditionalConfirm" />
<nop-action-confirmation asp-button-id="btnMarkPSC" asp-additional-confirm="Admin.Configuration.Currencies.AdditionalConfirm" />
<nop-alert asp-alert-id="currencyAlert" asp-alert-message="@T("Admin.Configuration.Currencies.Alert.Error")" />